package com.xiaoshou.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xiaoshou.service.QuestionService;
import com.xiaoshou.service.QuestionTypeService;
import com.xiaoshou.util.SpringUtils;


public class CreateIndexForQuestion implements ServletContextListener, Runnable {
	private SessionFactory sessionFactory;
	private QuestionService questionService;
	private QuestionTypeService questionTypeService;

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		
	}

	public void init(ServletContext servletContext) {
		sessionFactory = (SessionFactory) SpringUtils.getBean(servletContext,
				SessionFactory.class);
		questionService = (QuestionService) SpringUtils.getBean(servletContext, QuestionService.class);
		questionTypeService = (QuestionTypeService) SpringUtils.getBean(servletContext, QuestionTypeService.class);
	}

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		init(servletContextEvent.getServletContext());
		run();
	}

	public void run() {
		/**
		 * 创建索引,
		 */
		questionService.getIndexFile().indexFileForQuestion();
		
		/**
		 * 创建相应的问题文件夹
		 */
		//questionTypeService.createFolderOfQuestionType();
		
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public QuestionService getQuestionService() {
		return questionService;
	}

	@Autowired
	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public QuestionTypeService getQuestionTypeService() {
		return questionTypeService;
	}
	@Autowired
	public void setQuestionTypeService(QuestionTypeService questionTypeService) {
		this.questionTypeService = questionTypeService;
	}


}
